//
//  ViewController.swift
//  SwiftWebView
//
//  Created by LiynXu on 2016/9/27.
//  Copyright © 2016年 LiynXu. All rights reserved.
//

import UIKit

let screenRect = UIScreen.mainScreen().bounds

class ViewController: UIViewController {

    let webView:UIWebView = {
    
        let web = UIWebView()
        return web

    }()
    
    var myView:UIView = {
        let screenRect = UIScreen.mainScreen().bounds
        let rect = CGRectMake(0, screenRect.height-44, screenRect.width, 44)
        let _view = UIView(frame: rect)
        _view.backgroundColor = UIColor.blueColor()
        return _view
    }()
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.automaticallyAdjustsScrollViewInsets = false
        webView.frame = CGRectMake(0, 64, screenRect.width, screenRect.height-108)
        webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.2345.com")!, cachePolicy: .ReturnCacheDataElseLoad, timeoutInterval: 30))
        
        self.view.addSubview(webView)
        
        
        
       let backButton  = UIButton(type: .Custom)
        backButton.setTitle("< 返回", forState: .Normal)
        backButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
        backButton.frame = CGRectMake(0, 0, 70, 30)
        //backButton.titleLabel?.textAlignment = NSTextAlignment.Right
        backButton.addTarget(webView, action: #selector(UIWebView.goBack), forControlEvents: .TouchUpInside)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: backButton)
        
        let forwardButton = UIButton(type: .Custom)
        forwardButton.setTitle("前进 >", forState: .Normal)
        forwardButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
        forwardButton.frame = CGRectMake(0, 0, 70, 30)
        //forwardButton.titleLabel?.textAlignment = NSTextAlignment.Right
        forwardButton.addTarget(self.webView, action: #selector(self.webView.goForward), forControlEvents: .TouchUpInside)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: forwardButton)
        
        setUpMyView()
    }
    func setUpMyView() {
        //创建面板名称数组
        let array = ["goBack","goForward","reload","stopLoading"]
        //创建四个选择器
        let goback = #selector(webView.goBack)
        let goForward = #selector(webView.goForward)
        let reload = #selector(webView.reload)
        let stopLoading = #selector(webView.stopLoading)
        //将四个选择器存入选择器数组
        let funcArray = [goback,goForward,reload,stopLoading]
        for i in 0..<array.count {
            let button = UIButton(type: .Custom)
            let width = screenRect.width/4
            button.frame = CGRectMake(CGFloat(i)*width, 0, width, 44)
            button.setTitle(array[i], forState: .Normal)
            button.setTitleColor(UIColor.redColor(), forState: UIControlState.Selected)
            button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
            //绑定动作
            button.addTarget(self.webView, action: funcArray[i], forControlEvents: UIControlEvents.TouchUpInside)
            //button添加到面板上
            myView.addSubview(button)
        }
        self.view.addSubview(myView)

    }
}

